package com.vip.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public class LoginController extends MultiActionController {
	private final Log log = LogFactory.getLog(LoginController.class);

	@RequestMapping(method = RequestMethod.GET, value = "/login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/json;charset=UTF-8");
		int iResult = 0;
		/*
		 * String userName =
		 * ServletRequestUtils.getStringParameter(request,"userName"); String
		 * password =
		 * ServletRequestUtils.getStringParameter(request,"password"); String
		 * validateCode =
		 * ServletRequestUtils.getStringParameter(request,"validateCode");
		 */HttpSession session = request.getSession();
		PrintWriter out = null;
		// try {
		// out = response.getWriter();
		// if (CheckValidateCode(validateCode,
		// String.valueOf(session.getAttribute("validateCode"))) == 1) {
		// Admin admin =
		// ServiceManager.getLoginService().isLoginSuccess(userName,
		// password,validateCode);
		// if (admin != null) {
		// iResult = 1;
		// try{
		// session.removeAttribute("validateCode");
		// }
		// catch (Exception ex){
		// log.error("登录失败!" + ex.toString());
		// }
		// session.setAttribute("Admin", admin);
		// }
		// } else {
		// iResult = -1;
		// }
		// out.println("{\"result\":\"" + String.valueOf(iResult) + "\"}");
		// } catch (Exception ex1) {
		// out.println("{\"result\":\"登录失败！\"}");
		// log.error("登录失败!" + ex1.toString());
		// } finally {
		// out.close();
		// }
		System.out.println("login");
		return null;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mav = new ModelAndView();
		Enumeration<?> em = request.getSession().getAttributeNames();
		while (em.hasMoreElements()) {
			request.getSession().removeAttribute(em.nextElement().toString());
		}
		mav.setViewName("index");
		System.out.println("logout");
		return mav;
	}

}
